﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;

public partial class demo_fileUpload_fileUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // 获得程序路径
        string tempFile = Request.PhysicalApplicationPath;

        //找到目标文件对象
        HttpPostedFile uploadFile = Request.Files["Fdata"];

        Random random = new Random(DateTime.Now.Millisecond);
        string filePath = "";
        string newFileName = "";
        // 如果有文件, 则保存到一个地址
        if (uploadFile.ContentLength > 0)
        {
            filePath="Manage/FileUpload/Upload/" + "month_" + DateTime.Now.ToString("yyMM") + "/";
            newFileName = DateTime.Now.ToString("yyyyMMhhddmmss") + random.Next(10000) + Path.GetExtension(uploadFile.FileName);
            if (!Directory.Exists(tempFile + filePath))
                Directory.CreateDirectory(tempFile + filePath);
            uploadFile.SaveAs(tempFile + filePath + newFileName);
        }

        Response.Write(filePath + newFileName);    //可以返回一个JSON字符串, 在客户端做更多处理
    }
}